//
//  FeatureViewController.swift
//  funcode
//
//  Created by Wan Jiading on 2016/11/11.
//  Copyright © 2016年 WanJiading. All rights reserved.
//

import UIKit

class FeatureViewController: UITableViewController {

	let cellID = "FeatureCell"
	
    let attributeTextText = "富文本"
    let qrCodeText = "二维码"
    let memberAlertText = "MemberAlert"
    let toastCellText = "弹出框 放大、缩小"
	let charOrderCellText = "TableView 按照字母排序"
	let searchCellText = "Search 控制器"
	let drawCellText = "画图"
	let popCellText = "面向协议编程"
	let hitBubbleCellText = "打泡泡"
	let downWithPlayText = "边下边播"
	let downWithPlayText2 = "边下边播2"
	let transitionText = "转场动画"
	let musicListText = "底部弹出 - 音乐列表"
	let jsToIOSText = "JS与原生交互"
	let textViewText = "TextView"
	let collectionViewText = "CollectionView"
	let adPageText = "广告页"
	let KeyChainText = "钥匙串"
	let AliyunVideoTextHaveUIText = "阿里云视频带UI"
	let AliyunVideoTextCustomUIText = "阿里云视频自定义UI"
	let TestDBText = "测试数据库Playlist"
	
	var dataList: [String]!
	
	
    override func viewDidLoad() {
        super.viewDidLoad()

		initData()
		
		setupControls()
    }
	
	func setupControls() {
	
		setupLeftBarButtonItem()
		setupRightBarButtonItem()
	}
	
	
	///设置导航栏左侧按钮
	func setupLeftBarButtonItem() {
		
		self.navigationItem.leftBarButtonItem = self.getLeftBarButtonItem()
	}
	
	func getLeftBarButtonItem() -> UIBarButtonItem {
		
		let leftButton = UIButton.init(type: .custom)
		leftButton.setImage(UIImage.init(named: "nav_back_botton_custom"), for: .normal)
		leftButton.frame = CGRect.init(x: 0, y: 0, width: 22, height: 44)
		leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10)
		leftButton.addTarget(self, action: #selector(basePopView), for: .touchUpInside)
		leftButton.isHidden = false
		return UIBarButtonItem.init(customView: leftButton)
	}
	
	///设置导航栏右侧按钮
	func setupRightBarButtonItem() {
		
		self.navigationItem.rightBarButtonItem = self.getRightBarButtonItem()
	}
	
	/// 导航右边按钮
	func getRightBarButtonItem() -> UIBarButtonItem {
		
		let rightBtn = UIButton.init(type: .custom)
		rightBtn.frame = CGRect.init(x: 0, y: 0, width: 24, height: 34)
		rightBtn.backgroundColor = UIColor.clear
		rightBtn.addTarget(self, action: #selector(baseRightBtnClick), for: .touchUpInside)
		
		return UIBarButtonItem.init(customView: rightBtn)
	}
	

	
	///返回上一页
	func basePopView() {
		DebugLog("navItemLeftBtnClick")
		if self.navigationController != nil && self.navigationController!.childViewControllers.count > 1 {
			
			_ = self.navigationController?.popViewController(animated: true)
		} else {
			
			self.dismiss(animated: true, completion: nil)
		}
	}
	
	func baseRightBtnClick() {
		DebugLog("navItemRightBtnClick")
	}
	
	func initData() {
		self.navigationItem.title = "功能集合"
		
        dataList = [
            attributeTextText,
            qrCodeText,
            memberAlertText,
            AliyunVideoTextCustomUIText,
			TestDBText,
			adPageText,
			collectionViewText,
			toastCellText,
			charOrderCellText,
			searchCellText,
			drawCellText,
			popCellText,
			hitBubbleCellText,
			downWithPlayText,
			downWithPlayText2,
			transitionText,
			musicListText,
			jsToIOSText,
			textViewText,
			KeyChainText,
			AliyunVideoTextHaveUIText,
		]
	}
	
}

// MARK: - UITableViewDatasource UITableViewDelegate

extension FeatureViewController {
	
	override func numberOfSections(in tableView: UITableView) -> Int {
		return 1
	}
	
	override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		return dataList.count
	}
	
	override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
		return 50
	}
	
	override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
		let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as! FeatureCell
		cell.setTitle(dataList[indexPath.row])
		return cell
	}
	
	override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
		
		switch dataList[indexPath.row] {
		case toastCellText:
			let vc = ViewControllerUtil.viewController("Main", "AnimationToast") as! AnimationToastViewController
			vc.title = dataList[indexPath.row]
			pushVC(vc)
			
		case charOrderCellText:
			
			let vc = ViewControllerUtil.viewController("Main", "CharOrder") as! CharOrderViewController
			vc.selectedItem = { (c) in
				JDToast.show(c.area_name! + "，区号:" + c.area_code!)
			}
			vc.hidesBottomBarWhenPushed = true
			
			let nav = UINavigationController(rootViewController: vc)
			
			self.present(nav, animated: true, completion: { 
				
			})
			break
			
		case searchCellText:
			
			let vc = ViewControllerUtil.viewController("CountryArea", "CountryArea") as! CountryAreaViewController
			vc.selectedItem = { (c) in
				JDToast.show(c.area_name! + "，区号:" + c.area_code!)
			}
			vc.hidesBottomBarWhenPushed = true
			
			let nav = UINavigationController(rootViewController: vc)
			
			self.present(nav, animated: true, completion: {
				
			})
			break
			
		case drawCellText:
			pushVC(DrawViewController())
			
		case popCellText:
			pushVC(PopViewController())
			
		case hitBubbleCellText:
			pushVC(HitBubbleViewController())
			
		case downWithPlayText:
			pushVC(AudioPlayerViewController())
			
		case downWithPlayText2:
			pushVC(TBPlayerViewController())
			
		case transitionText:
			pushVC(AnimationViewController())
			
		case musicListText:
			pushVC(BlurViewController())
			
		case jsToIOSText:
			pushVC(TestWebViewController())
			
		case textViewText:
			let vc = UIStoryboard.init(name: "SNS", bundle: nil).instantiateViewController(withIdentifier: "AddFeed")
			pushVC(vc)
			
		case collectionViewText:
			let vc = UIStoryboard(name: "Radio", bundle: nil).instantiateInitialViewController()!
			pushVC(vc)
			
		case adPageText:
			pushVC(AdPageViewController())
			
		case KeyChainText:
			pushVC(KeyChainViewController())
			
		case AliyunVideoTextHaveUIText:
//			pushVC(AliyunVideoHaveUIViewController())
            break
			
		case AliyunVideoTextCustomUIText:
			let vc = UIStoryboard(name: "Cartoon", bundle: nil).instantiateViewController(withIdentifier: "AliyunVideoTextCustomUITextViewController")
			pushVC(vc)
			
		case TestDBText:
            pushVC(PlaylistDBViewController())
			
        case memberAlertText:
            pushVC(MemberAlertViewController())
            
        case qrCodeText:
            let vc = UIStoryboard(name: "QRCode", bundle: nil).instantiateViewController(withIdentifier: "QRCode")
            pushVC(vc)
            
        case attributeTextText:
            let vc = UIStoryboard(name: "AttributeText", bundle: nil).instantiateViewController(withIdentifier: "AttributeText")
            pushVC(vc)
            
            
		default:
			break
		}
	}
	
    private func pushVC(_ vc: UIViewController) {
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
